﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.MailGun
{
    public class Webhook
    {
        public static IRestResponse AddDomainTracking(string api_key, List<string> url, string domain)
        {
            
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2/";
            client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                               api_key);
            RestRequest request = new RestRequest();
            request.Resource = "domains/"+domain+"/webhooks";
            request.AddParameter("id", "click");
            request.AddParameter("url", url[0]);
            request.Method = Method.POST;
            IRestResponse response = client.Execute(request);
            string re = response.Content;

            request = new RestRequest();
            request.Resource = "domains/" + domain + "/webhooks";
            request.AddParameter("id", "open");
            request.AddParameter("url", url[1]);
            request.Method = Method.POST;
            client.Execute(request);

            request = new RestRequest();
            request.Resource = "domains/" + domain + "/webhooks";
            request.AddParameter("id", "deliver");
            request.AddParameter("url", url[2]);
            request.Method = Method.POST;
            client.Execute(request);

            request = new RestRequest();
            request.Resource = "domains/" + domain + "/webhooks";
            request.AddParameter("id", "drop");
            request.AddParameter("url", url[3]);
            request.Method = Method.POST;
            client.Execute(request);

            request = new RestRequest();
            request.Resource = "domains/" + domain + "/webhooks";
            request.AddParameter("id", "bounce");
            request.AddParameter("url", url[3]);
            request.Method = Method.POST;
            return client.Execute(request);
        }
    }
}
